﻿using Yz.Base;

namespace Yz.Core.Extensions
{
    public static class VmReturnExtension
    {
        /// <summary>
        /// 把操作code转换成VmReturn
        /// </summary>
        /// <param name="retCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmReturn ToVmReturn(this ReturnCode retCode, string msg)
        {
            if (retCode == ReturnCode.NoExit || retCode == ReturnCode.Success)
            {
                return new VmReturn() { Suc = true, Msg = msg };
            }
            return new VmReturn() { Suc = false, Msg = msg };
        }
        /// <summary>
        /// 把操作code转换成VmReturn
        /// </summary>
        /// <param name="retCode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static VmReturn ToVmReturn(this ReturnCode retCode)
        {
            if (retCode == ReturnCode.NoExit)
            {
                return new VmReturn() { Suc = true, Msg = "您要操作的数据不存在或者您没有权限操作！" };
            }
            else if (retCode == ReturnCode.Exit)
            {
                return new VmReturn() { Suc = true, Msg = "您要操作的数据已存在！" };
            }
            else if (retCode == ReturnCode.Success)
            {
                return new VmReturn() { Suc = true, Msg = "操作成功！" };
            }
            return new VmReturn() { Suc = false, Msg = "操作失败" };
        }

        public static VmPageList<T> ToVmPageList<T>(this ReturnList<T> list) where T : ViewModel
        {
            return new VmPageList<T>()
            {
                Total = list.Total,
                List = list.List,
                Suc = list.Total > 0,
                Msg = list.Total > 0 ? "" : "没有查询到数据！"
            };
        }

        public static VmList<T> ToVmList<T>(this ReturnList<T> list) where T : ViewModel
        {
            return new VmList<T>()
            {
                List = list.List,
                Suc = list.List.Count > 0,
                Msg = list.List.Count > 0 ? "" : "没有查询到数据！"
            };
        }

        public static VmData<T> ToVmData<T>(this T data) where T : ViewModel
        {
            return new VmData<T>() { Data = data, Suc = data != null, Msg = data == null ? "数据不存在！" : "" };
        }
        public static VmData ToVmData(this object data)
        {
            return new VmData() { Data = data, Suc = data != null, Msg = data == null ? "数据不存在！" : "" };
        }
        public static VmList ToVmList(this object data)
        {
            return new VmList() { List = data, Suc = data != null, Msg = data == null ? "数据不存在！" : "" };
        }
        public static VmPageList ToVmPageList(this object data)
        {
            return new VmPageList() { List = data, Total = 0, Suc = data != null, Msg = data == null ? "数据不存在！" : "" };
        }
        public static VmCount ToVmCount(this int total)
        {
            return new VmCount() { Suc = total > 0, Total = total, Msg = total > 0 ? "" : "数量为0！" };
        }

        public static VmList<T> ToVmList<T>(this List<T> list) where T : ViewModel
        {
            return new VmList<T>()
            {
                List = list,
                Suc = list.Count > 0,
                Msg = list.Count > 0 ? "" : "没有查询到数据！"
            };
        }
    }
}
